﻿using UnityEngine;
using Chat;
using Dict.DataModel;

namespace UI
{
    public class UIUnionFightRunMapEvent : MonoBehaviour
    {
        UIUnionFightRunMap runMapPage;


        void Awake()
        {
            runMapPage = GetComponent<UIUnionFightRunMap>();

        }

        public void OpenOrderPage(object isOpen)
        {
         
            var tween = runMapPage.orderPage.GetComponentInChildren<TweenPosition>();
            tween.ResetToBeginning();
            var orderPage = runMapPage.orderPage;

            if (isOpen != null)
            {
                orderPage.mask.SetActive(true);
                tween.PlayForward();
            }
            else
            {
                orderPage.mask.SetActive(false);
                tween.PlayReverse();
            }

        }
        /// <summary>
        /// 打开工会战聊天界面
        /// </summary>
        public void OpenChatPage()
        {
            runMapPage.chatPage.Init();
        }

        public void OpenBuildingStrengthenPage()
        {
            runMapPage.buildingPage.SendCSMsg(new TableUnionFightNpc());
        }

        public void OpenCombatLogPage()
        {
            runMapPage.combatLogPage.Init();
        }

        public void OpenTaskPage()
        {
            runMapPage.taskPage.Init(runMapPage);
        }

        public void OpenRecycleBinNPCEvent()
        {
            if (!runMapPage.bagFullTip.activeInHierarchy)
                return;
            BoxManager.CreateMessageBoxWithCofirmAndCancelButton(TableTextBlo.GetText("union_fight_recycleBin_text"), (b) => 
            {
                if (b)
                {
                    var npcs = Dict.Blo.DictUnionFightNpcBlo.GetAll();
                    var uuid = "";
                    int selfCamp = UIUnionFightRunMap.GetNetData().charactorManager.GetSelfCharactor().Camp;
                    foreach (var npc in npcs)
                    {
                        if (selfCamp == npc.Camp && (int.Parse(npc.UFNId) % 4 == 2))
                            uuid = "npc_" + npc.UFNId;
                    }
                    var npcUI = UIUnionFightRunMap.GetNetData().charactorManager.GetCharactor(uuid).ui as Room.RCUIUnionFightNpc;
                    npcUI.ClickEvent();
                }
            });
        }
        public void OnExit()
        {
            var str = TableTextBlo.GetText("union_fight_ask_exit");
            BoxManager.CreateMessageBoxWithCofirmAndCancelButton(str,true, (b) =>
            {
                if (b)
                {
                    ManagerController.Instance.TrySceneTransfer(ServerScene.Town, Vector3.zero, "", () =>
                    {
                        GetComponent<UIUnionFightRunMap>().Close();
                        PageManager.Instance.OpenPage("TownPage", "");
                    });
                }
            },TableTextBlo.GetText("exit_battle"));
        }

        /// <summary>
        /// 点击挂机
        /// </summary>
        public void OnHookClick()
        {
            var net = Room.NetUnionFightBase.GetNet();
            if(net != null)
            {
                net.SendCSUFOnHook(!UnionFightData.IsOnHook);
            }
        }


#if UNITY_EDITOR
        void OnGUI()
        {
            if(GUI.Button(new Rect(50,0,100,40),"GM"))
            {
                var msg = new CSChatTcp();
                msg.content = "!reset.fight.union";
                msg.playerId = StaticData.playerData.playerId;
                msg.chatType = (int)ChatType.World;
                NetAPI.SendSocket(OpDefine.CSChatTcp, msg);
            }

            if (GUI.Button(new Rect(160, 0, 100, 40), "脱离卡死"))
            {
                var msg = new CSUFRefreshElement();
                msg.guid = StaticData.playerData.playerId;
                NetAPI.SendSocket(OpDefine.CSUFRefreshElement, msg);
            }

            //if (GUI.Button(new Rect(50, 50, 100, 40), "上交资源"))
            //{
            //    UIUnionFightRunMap.GetNetData().SendCSUFPlayerUpResource();
            //}

            //if (GUI.Button(new Rect(50, 100, 100, 40), "开启车场"))
            //{
            //    var myCamp = UIUnionFightRunMap.GetNetData().netBattle.camp;
            //    var factorys = Dict.Blo.DictUnionFightFactoryBlo.GetAll();

            //    foreach(var factory in factorys)
            //    {
            //        if(factory.IsField==1 && factory.Camp==myCamp)
            //        {
            //            //UIUnionFightRunMap.GetNetData().SendCSUFFieldCariotFactoryOpen(factory.UFFId+"");
            //            BoxManager.CreatePopupTextBox("请求开启车场：" + factory.UFFId + " camp:" + myCamp);
            //            break;
            //        }
            //    }

            //}

            //if(GUI.Button(new Rect(50, 150, 100, 40), "自己加血"))
            //{
            //    //ufitem_001
            //    UIUnionFightRunMap.GetNetData().SendCSUFPlayerUseItem("ufitem_004");
            //}

            //if (GUI.Button(new Rect(50, 200, 100, 40), "请求商品"))
            //{
            //    //ufitem_001
            //    UIUnionFightRunMap.GetNetData().SendCSUFShopList();
            //}

            //if (GUI.Button(new Rect(150, 200, 100, 40), "购买商品"))
            //{
            //    //ufitem_001
            //    UIUnionFightRunMap.GetNetData().SendCSUFPlayerBuyItem("shop_ufitem_001");
            //}

        }
#endif
    }
}